package com.panshi.controller.qa;

import com.panshi.api.*;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Messag;
import com.panshi.vo.qa.Pages;
import com.panshi.vo.qa.QuestionAudit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
public class QuestionAuditController {
    @Autowired
    IQuestionAuditApi questionAuditApi;

    @Autowired
    IRabbitMQApi rabbitMQApi;

    @Autowired
    IMessagApi messagApi;

    /*QuestionAudit(问题审核)*/

    /**
     * 问题审核添加
     * @param questionAudit
     * @return
     */
    @PostMapping("/questionAudit/add")
    @ResponseBody
    DataResultQA addQuestionAudit(@RequestBody QuestionAudit questionAudit){
        log.info("questionAudit:{}",questionAudit);
        DataResultQA dataResultQA = questionAuditApi.addQuestionAudit(questionAudit);

        if(dataResultQA.isBoo()){//发送站内消息
            Messag messag = new Messag();
            //用户id
            messag.setUserId(questionAudit.getQUserId());
            //问题
            messag.setTitle(questionAudit.getQContent());
            //表示文章类型
            messag.setCheckType(Messag.QUESTION_TYPE);

            messagApi.addMessag(messag);
        }



        return dataResultQA;
    }

    /**
     * 问题审核分页查询
     * @param pages
     * @return
     */
    @PostMapping("/questionAudit/inquirePages")
    @ResponseBody
    DataResultQA<Pages<QuestionAudit>> inquirePagesQuestionAudit(@RequestBody Pages pages){
        DataResultQA<Pages<QuestionAudit>>  dataResultQA= questionAuditApi.inquirePagesQuestionAudit(pages);
        return dataResultQA;
    }

    /**
     * 查询记录总数
     * @param pages
     * @return
     */
    @PostMapping("/inquireQuestionAuditSum")
    @ResponseBody
    DataResultQA<Integer> inquireQuestionAuditSum(@RequestBody Pages pages){
        DataResultQA<Integer> dataResultQA = questionAuditApi.inquireQuestionAuditSum(pages);
        return dataResultQA;
    }

    /**
     * 问题审核根据id查询
     * @param id
     * @return
     */
    @GetMapping("/questionAudit/inquireById/{id}")
    @ResponseBody
    QuestionAudit inquireByIdQuestionAudit(@PathVariable int id){
        QuestionAudit questionAudit = questionAuditApi.inquireByIdQuestionAudit(id);
        return questionAudit;
    }

    /**
     * 问题审核修改
     * @param questionAudit
     * @return
     */
    @PostMapping("/questionAudit/update")
    @ResponseBody
    DataResultQA updateQuestionAudit(@RequestBody QuestionAudit questionAudit){
        DataResultQA dataResultQA = questionAuditApi.updateQuestionAudit(questionAudit);
        return dataResultQA;
    }

    /**
     * 问题审核删除（标记删除）
     * @param id
     * @return
     */
    @GetMapping("/questionAudit/delete/{id}")
    @ResponseBody
    DataResultQA deleteQuestionAudit(@PathVariable int id){
        DataResultQA dataResultQA = questionAuditApi.deleteQuestionAudit(id);
        return dataResultQA;
    }

    /**
     * 问题审核(审核)
     * @param questionAudit
     * @return
     */
    @PostMapping("/questionAudit/updateState")
    @ResponseBody
    DataResultQA updateState(@RequestBody QuestionAudit questionAudit){
        DataResultQA dataResultQA= questionAuditApi.updateStateQuestionAudit(questionAudit);
        //将审核信息发送给用户
        if(dataResultQA.isBoo()){
            rabbitMQApi.sendQuestionAudit(questionAudit);
        }
        return dataResultQA;
    }

    /**
     * 用于用户查询自己发布的问题审核
     * @param userId
     * @return
     */
    @PostMapping("/questionAudit/inquireMyQuestionAudit/{userId}")
    @ResponseBody
    DataResultQA<List<QuestionAudit>> inquireMyQuestionAudit(@PathVariable int userId){
        DataResultQA<List<QuestionAudit>> dataResultQA = questionAuditApi.inquireMyQuestionAudit(userId);
        return dataResultQA;
    }
}
